<?php

class article extends data
{
   protected $main_table      = 'meccano_articles';
   protected $translate_table = 'meccano_articles_translate';

   public $name      = 'article';
   public $views     = 0;
   public $deleted   = NULL;
   public $category  = NULL;

   /**
    * Constructor
    *
    * @param integer $id
    * @param integer $category_id
    */
   public function  __construct($id = NULL, $category_id = NULL)
   {
      parent::__construct();
      $this->cat = NULL;

      if(!empty($id))
      {
         return $this->load($id, $category_id);
      } else
      {
         return $this->loadEmpty();
      }
   }


   /**
    * Article Loader
    *
    * @param integer $id - ID of the article
    * @param integer $category_id - ID of the article's category
    * @return object
    */
   public function load($id, $category_id = NULL)
   {
      if(!is_numeric($id)) return FALSE;
      $DB = database::getInstance();

      $DB->sql = 'SELECT `t1`.*, `t2`.*
         FROM `'. $this->main_table .'` AS `t1`, `'. $this->translate_table .'` AS `t2`
         WHERE `t1`.`id` = `t2`.`id`
         AND `t1`.`deleted` IS NULL
         AND `t1`.`id` = '. $id;
      if(is_numeric($category_id))
      {
         $DB->sql .= ' AND `t1`.`cat` = "'. $category_id . '"';
      }
      $data = $DB->DB_Fetch();
      $this->loadFromArray($data);

      if(!empty($this->id))
      {
         $DB->sql = 'UPDATE `'. $this->main_table .'` SET `views` = `views` + 1 WHERE `id` = '. $this->id;
         $DB->DB_Query();
      }
   }


   public function loadFromArray($data)
   {
      if(!empty($data))
      {
         $data_one = reset($data);

         $this->id       = $data_one['id'];
         $this->position = $data_one['position'];
         $this->cdate    = $data_one['cdate'];
         $this->mdate    = $data_one['mdate'];
         $this->views    = $data_one['views'];
         $this->deleted  = $data_one['deleted'];
         $this->setCategory($data_one['cat']);

         foreach($data AS $data_one)
         {
            parent::load($data_one, $data_one['lang']);
         }

         return TRUE;
      }

      return FALSE;
   }


   public function save($data)
   {
      $sql = parent::save($data);
      if(empty($sql['main'])) return FALSE;

      $DB = database::getInstance();
      $sql_main = '';

      if(empty($this->id))
      {
         $sql['main'][] = '`cat` = '. $this->cat;
         $sql['main'][] = '`cdate` = NOW()';
         $sql['main'][] = '`user` = '. session::user()->id;
         $sql_main = 'INSERT INTO `'. $this->main_table .'` SET'. implode(', ', $sql['main']);

         # SHIFT POSITION
         $DB->sql = 'UPDATE `'. $this->main_table .'` SET `position` = `position` + 1 WHERE `cat` = '. $this->cat;
         $DB->DB_Query();
      } else
      {
         $sql_main = 'UPDATE `'. $this->main_table .'` SET'. implode(', ', $sql['main']) .' WHERE `id` = '. $this->id;
      }

      $DB->DB_StartTransaction();
      
      # MAIN QUERY
      $DB->sql = $sql_main;
      $DB->DB_Query();

      # UPDATE ID
      if(empty($this->id))
      {
         $this->id = $DB->DB_LastID();
      }

      # TRANSLATION QUERIES
      foreach($sql['translate'] AS $l => $sql_translate)
      {
         $sql_translate  = 'INSERT INTO `'. $this->translate_table .'` SET `id` = '. $this->id .', `lang` = '. $l .', '. implode(', ', $sql_translate) .
            ' ON DUPLICATE KEY UPDATE '. implode(', ', $sql_translate);

         $DB->sql = $sql_translate;
         $DB->DB_Query();
      }

      if($DB->DB_EndTransaction())
      {
         $this->saveFieldsets($data);

         return TRUE;
      }

      return FALSE;
   }


   protected function loadEmpty($fields = NULL)
   {
      $FIELDS = fields::getInstance();
      $fields = $FIELDS->splitFields(array('belong'=>'a', 'active'=>TRUE));
      return parent::loadEmpty($fields);
   }

   public function setCategory($id)
   {
      $this->category = tree_static::getNode($id);
      if(!empty($this->category))
      {
         $this->cat = $id;
         $FIELDS = fields::getInstance();
         $fields = $FIELDS->splitFields(array('node'=>$this->category->id, 'belong'=>'a', 'active'=>TRUE));
         parent::loadEmpty($fields);
      }
   }

   public function getFields($alias = null, $tab = null)
   {
      $node   = $this->category->id;
      $filter = array('belong'=>'a', 'active'=>TRUE);
      return parent::getFields($node, $filter);
   }

   public function href()
   {
      if(!empty($this->_href)) return $this->_href;

      $this->_href = substr($this->url(), 0, -1) .'.html';

      return $this->_href;
   }

   public function url()
   {
      if(!empty($this->_url)) return $this->_url;

      $this->_url = $this->category->url() . $this->id .'/';
      return $this->_url;
   }

   public function countComments()
   {
      $CM = new comments();
      $this->countComments = $CM->countComments($this->id);

      return $this->countComments;
   }
}